import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import '../demo/ui_demo.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
class MyDemo extends StatelessWidget implements Demo {
  MyDemo(this.title);
  @override
  String title;
  @override
  Widget build(BuildContext context) {
    return new HomePage(title: this.title,);
  }
}

class HomePage extends StatelessWidget {
  String title;
  HomePage({this.title});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(this.title),
        actions: <Widget>[
          IconSource("shared_preferences","24"),
        ],
      ),
      body:SharedPreferencedDemo(),
    );
  }
}
class SharedPreferencedDemo extends StatefulWidget {
  @override
  _SharedPreferencedDemoState createState() => _SharedPreferencedDemoState();
}

class _SharedPreferencedDemoState extends State<SharedPreferencedDemo> {
  String _value="";
   String filePath;
  SharedPreferences sp;
  TextEditingController controller=TextEditingController();
  @override
  void initState() {
    super.initState();
    _initSharedPreference();

  }
  String timestamp() => DateTime.now().millisecondsSinceEpoch.toString();
  _initSharedPreference() async{
    sp=await SharedPreferences.getInstance();
    //Directory directory=await getApplicationDocumentsDirectory();
    Directory directory=await getTemporaryDirectory();
    String tempDir=directory.path;
    filePath=tempDir+"/${timestamp()}.txt";
  }
  _readValue(){
    setState(() {
      _value=sp.getString("key");
    });
  }
  _saveToFile() async{

    File file=File(filePath);
    file.writeAsStringSync(controller.text,mode: FileMode.append);
  }
  _readFromFile() async{
    File file=File(filePath);
    file.readAsString().then((String content){
      setState(() {
        _value="filePath:$filePath\n content:$content";
      });
    });

  }
  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: <Widget>[
          TextField(
            controller: controller,
            decoration: InputDecoration(
              hintText: "please input"
            ),
          ),
          Wrap(
            spacing: 8.0,
            children: <Widget>[
              OutlineButton(
                child: Text("save"),
                onPressed: (){
                  sp.setString("key", controller.text);
                },
              ),
              OutlineButton(
                child: Text("read"),
                onPressed: _readValue,
              ),
            ],
          ),
          Wrap(
            spacing: 8.0,
            children: <Widget>[
              OutlineButton(
                child: Text("saveToFile"),
                onPressed: _saveToFile,
              ),
              OutlineButton(
                child: Text("readFromFile"),
                onPressed: _readFromFile,
              )
            ],
          ),
          ListTile(
            title:Text("结果") ,
            subtitle: Text(_value),
          ),
        ],
      ),
    );
  }
}


